<?php
/**
 * Created by PhpStorm.
 * Author: iHammer
 * Date: 2018/5/23 0023
 * Time: 13:57
 * Name: 直播接口
 */
namespace App\Api\Helper;
class TxLive
{

    /**
     * @name 获取推流地址 如果不传key和过期时间，将返回不含防盗链的url
     * @param $bizId bizId 您在腾讯云分配到的bizid
     * @param $streamId streamId 您用来区别不同推流地址的唯一id
     * @param null $key key 安全密钥
     * @param null $time time 过期时间 sample 2016-11-12 12:00:00
     * @return string url
     * @author ihammer
     */
    public static function getPushUrl($bizId, $streamId, $key = null, $time = null){

        if($key && $time){
            $txTime = strtoupper(base_convert(strtotime($time),10,16));
            //txSecret = MD5( KEY + livecode + txTime )
            //livecode = bizid+"_"+stream_id  如 8888_test123456
            $livecode = $bizId."_".$streamId; //直播码
            $txSecret = md5($key.$livecode.$txTime);
            $ext_str = "?".http_build_query(array(
                    "bizid"=> $bizId,
                    "txSecret"=> $txSecret,
                    "txTime"=> $txTime
                ));
        }
        return "rtmp://".$bizId.".livepush.myqcloud.com/live/".$livecode.(isset($ext_str) ? $ext_str : "");
    }

    /**
     * @param $bizId bizId 您在腾讯云分配到的bizid
     * @param $streamId streamId 您用来区别不同推流地址的唯一id
     * @return array url
     * @name 获取播放地址
     * @author ihammer
     */
    public static function getPlayUrl($bizId, $streamId){
        $livecode = $bizId."_".$streamId; //直播码
        return array(
            "rtmp://".$bizId.".liveplay.myqcloud.com/live/".$livecode,
            "http://".$bizId.".liveplay.myqcloud.com/live/".$livecode.".flv",
            "http://".$bizId.".liveplay.myqcloud.com/live/".$livecode.".m3u8"
        );
    }

}